<?php

include "../controller/ConectBanco.php";

    $page = isset($_POST['page']) ? $_POST['page'] : 1;
    $rp = isset($_POST['rp']) ? $_POST['rp'] : 10;
    $sortname = isset($_POST['sortname']) ? $_POST['sortname'] : 'name';
    $sortorder = isset($_POST['sortorder']) ? $_POST['sortorder'] : 'desc';
    $query = isset($_POST['query']) ? $_POST['query'] : false;
    $qtype = isset($_POST['qtype']) ? $_POST['qtype'] : false;
    $idUsuario = isset($_POST['idUsuario']) ? $_POST['idUsuario'] : false;

   /*  if(isset($_GET['Add'])){ // this is for adding records

        $rows = $_SESSION['Example4'];
        $rows[$_GET['EmpID']] = 
        array(
            'name'=>$_GET['Name']
            , 'favorite_color'=>$_GET['FavoriteColor']
            , 'favorite_pet'=>$_GET['FavoritePet']
            , 'primary_language'=>$_GET['PrimaryLanguage']
        );
        $_SESSION['Example4'] = $rows;

    }
    elseif(isset($_GET['Edit'])){ // this is for Editing records
        $rows = $_SESSION['Example4'];
        
        unset($rows[trim($_GET['OrgEmpID'])]);  // just delete the original entry and add.
        
        $rows[$_GET['EmpID']] = 
        array(
            'name'=>$_GET['Name']
            , 'favorite_color'=>$_GET['FavoriteColor']
            , 'favorite_pet'=>$_GET['FavoritePet']
            , 'primary_language'=>$_GET['PrimaryLanguage']
        );
        $_SESSION['Example4'] = $rows;
    }
    elseif(isset($_GET['Delete'])){ // this is for removing records
        $rows = $_SESSION['Example4'];
        unset($rows[trim($_GET['Delete'])]);  // to remove the \n
        $_SESSION['Example4'] = $rows;
    }
    else{ */
    $result = mysql_query("select idDevice,idPackage,realdatetime,velocidade,alarm,charge,acc,voltage,gsmsignal,
							(select nomePonto from ponto where idDevice = pk.idDevice) as nomePonto
 							from package pk
							where (idDevice,idPackage) in (select distinct idDevice,max(idPackage) as idPackage
															 from device dv
															NATURAL LEFT JOIN package 
															NATURAL LEFT JOIN ponto pt
															 where idUsuario = $idUsuario
														  group by idDevice)")or die (mysql_error());
    while($row = mysql_fetch_array($result)){
    
    	$rows[$row["idDevice"]] = array( "idDevice"		=>	$row["idDevice"]
    			 					    ,"idPackage" 	=>	$row["idPackage"]
    									,"velocidade"	=>  $row["velocidade"]
    									,"alarm"		=>  $row["alarm"]
    									,"charge"		=>  $row["charge"]
    									,"acc"			=>  $row["acc"]
    									,"voltage"		=>  $row["voltage"]
    									,"gsmsignal"	=>  $row["gsmsignal"]
    									,"realdatetime"	=>  $row["realdatetime"]
    									,"nomePonto"	=>  $row["nomePonto"]  	);
    }
    
       /*  if(isset($_SESSION['Example4'])){ // get session if there is one
            $rows = $_SESSION['Example4'];
        }
        else{ // create session with some data if there isn't
            $rows[1] = array('name'=>'Tony',   'favorite_color'=>'green',  'favorite_pet'=>'hamster',   'primary_language'=>'english');
            $rows[2] = array('name'=>'Mary',   'favorite_color'=>'red',    'favorite_pet'=>'groundhog', 'primary_language'=>'spanish');
            $rows[3] = array('name'=>'Seth',   'favorite_color'=>'silver', 'favorite_pet'=>'snake',     'primary_language'=>'french');
            $rows[4] = array('name'=>'Sandra', 'favorite_color'=>'blue',   'favorite_pet'=>'cat',       'primary_language'=>'mandarin');
            $_SESSION['Example4'] = $rows;
        }
 */

		
        header("Content-type: application/json");
        $jsonData = array('page'=>$page,'total'=>0,'rows'=>array());
        foreach($rows AS $rowNum => $row){
        	
        	$imgAlarm = $row["alarm"] == 1 ? '<img src="../images/flexigrid/check.png" width="17" height="17"></img>':'<img src="../images/flexigrid/warning.png" width="17" height="17"></img>';
        	$imgCharge = $row["charge"] == 1 ? '<img src="../images/flexigrid/check.png" width="17" height="17"></img>':'<img src="../images/flexigrid/warning.png" width="17" height="17"></img>';
        	$imgAcc = $row["acc"] == 1 ? '<img src="../images/flexigrid/check.png" width="17" height="17"></img>':'<img src="../images/flexigrid/warning.png" width="17" height="17"></img>';
        	$imgVoltage = $row["voltage"] == 1 ? '<img src="../images/flexigrid/check.png" width="17" height="17"></img>':'<img src="../images/flexigrid/warning.png" width="17" height="17"></img>';
        	$imgGsmSignal = $row["gsmsignal"] == 1 ? '<img src="../images/flexigrid/check.png" width="17" height="17"></img>':'<img src="../images/flexigrid/warning.png" width="17" height="17"></img>';
        	
            //If cell's elements have named keys, they must match column names
            //Only cell's with named keys and matching columns are order independent.
            $entry = array('id' => $rowNum,
                'cell'=>array(
                   						 "idDevice"		=>	$row["idDevice"]
    			 					    ,"idPackage" 	=>	$row["idPackage"] 
    									,"velocidade"	=>  $row["velocidade"]
    									,"alarm"		=>  $imgAlarm
    									,"charge"		=>  $imgCharge
    									,"acc"			=>  $imgAcc
    									,"voltage"		=>  $imgVoltage
    									,"gsmsignal"	=>  $imgGsmSignal
    									,"realdatetime"	=>  $row["realdatetime"]
                						,"nomePonto"	=>  '<a href="#" id="'.$row["idDevice"].'" ondblclick="setCenter(this)">'.$row["nomePonto"].'</a>'  )
            );
            $jsonData['rows'][] = $entry;
        }
        $jsonData['total'] = count($rows);
        echo json_encode($jsonData);

//}